function UploadHandle() {

}
UploadHandle.prototype = {}

function ImgCropper() {
    //this.cropImg()
}

ImgCropper.origin_width = 0;
ImgCropper.ieVersion = Number(document.documentMode);
ImgCropper.origin_height = 0;
ImgCropper.SrcOrigin = null;
ImgCropper.SrcCroped = null;
ImgCropper.prototype = {
    init: function () {
        var _this = this;
        $('#cropper-input').on('change', function () {
            if (typeof FileReader !== 'undefined') {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var url = reader;
                    console.log(e);
                    _this.setImgSrc(e.currentTarget.result);
                    // 上传完成,回调
                    _this.cropHandle();
                };

                // 获取当前图片
                var imgFiles = $(this).get(0).files;
                if (imgFiles.length === 0) {
                    return;
                }

                // todo 格式,大小过滤

                // base64模式
                reader.readAsDataURL(imgFiles[0]);
            } else {
                // < IE10
                $(this).select();
                $(this).blur();

                var src = document.selection.createRange().text;
                document.selection.empty();

                _this.setImage(src);
            }
        });
    },

    setImgSrc: function (src) {
        ImgCropper.SrcOrigin = src;
        $('.crop-wrap .origin-img').attr('src', src);
    },
    cropHandle: function () {
        var _this = this;
        var canvas = $('<canvas>')[0];
        var ctx = canvas.getContext("2d");

        img = new Image();
        img.src = ImgCropper.SrcOrigin;
        img.onload = function () {
            canvas.width = 400;
            canvas.height = 300;
            ctx.drawImage(img, 200, 100, img.width, img.height, 80, 100, 200, 200);
            var newSrc = canvas.toDataURL();
            ImgCropper.SrcOrigin = newSrc;
            $('.crop-img').attr('src', newSrc);
            console.log(ImgCropper.SrcOrigin);
        };
    }
};

function getImageSize(src, callback) {
    // todo ie 10 以下
    if (ieVersion < 10) {
        if (!preLoadElement) {
            preLoadElement = document.createElement('div');
            preLoadElement.style.position = 'absolute';
            preLoadElement.style.width = '1px';
            preLoadElement.style.height = '1px';
            preLoadElement.style.left = '-9999px';
            preLoadElement.style.top = '-9999px';
            preLoadElement.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)';
            document.body.insertBefore(preLoadElement, document.body.firstChild);
        }

        preLoadElement.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;

        var size = {
            width: preLoadElement.offsetWidth,
            height: preLoadElement.offsetHeight
        };

        if (typeof callback === 'function') {
            callback(size);
        }
    } else {
        var image = new Image();
        image.onload = function () {
            var size = {
                width: image.width,
                height: image.height
            };
            if (typeof callback === 'function') {
                callback(size);
            }
        };
        image.src = src;
    }
}
